<?php
namespace Model;



use Model\Factory\TableGatewayFactory;

class Role extends AbstractModel
{
    public $id;
    public $code;
    public $name;
    public $description;
    public $modifiable;
    public $created_date;
    public $created_by;
    public $active;

    private $_allPrivilege;

    public function  getAllPrivilege()
    {
        $privilegeTable = TableGatewayFactory::getPrivilegeTable();
        $select = $privilegeTable->getDbSqlSelect();
        $select->join(array('rp'=>'role_privilege'), 'rp.privilege_id = privilege.id', array())
        ->where(array('role_id'=>$this->id));
        $this->_allPrivilege = $privilegeTable->getTableGateway()->selectWith($select);
        return $this->_allPrivilege;
    }

    public function isHavingPrivilege($privilegeId){
        $allPrivilege = $this->getAllPrivilege();
        foreach ($allPrivilege as $myPrivilege) {
            if($myPrivilege->id == $privilegeId)
                return true;
        }
        return false;
    }

    public function  getAllPrivilegeByType($privilegeTypeCode)
    {

        TableGatewayFactory::getPrivilegeTable();
        $privilegeTable = TableGatewayFactory::getPrivilegeTypeTable();
        $privilegeType = $privilegeTable->getByColumn('code', $privilegeTypeCode)->current();
        $select = $privilegeTable->getDbSqlSelect();
        $select->join(array('rp'=>'role_privilege'), 'rp.privilege_id = privilege.id', array())
            ->where(array('role_id'=>$this->id, 'type_id'=>$privilegeType->id));
        return $privilegeTable->getTableGateway()->selectWith($select);

    }

    public function updatePrivilege($privilegeId, $checked)
    {
        $privilegeTable = TableGatewayFactory::getPrivilegeTable();
        $privilegeItem = $privilegeTable->getPrivilege($privilegeId);
        $privilegeItem->updateRole($this->id, $checked);
    }

    public function detachUser($userId)
    {
        $userRoleTable = TableGatewayFactory::getUserRoleTable();
        $userRoleTable->getTableGateway()->delete(
            array('person_id'=>$userId, 'role_id'=>$this->id));
    }

    public function attachUser($userId)
    {

        $userRoleTable = TableGatewayFactory::getUserRoleTable();
        $select = $userRoleTable->getDbSqlSelect()
            ->where(array('person_id'=>$userId, 'role_id'=>$this->id));

        $allCouple = $userRoleTable->getTableGateway()->selectWith($select);
        if($allCouple->count() > 0)
            return null;
        return $userRoleTable->insert(array('person_id'=>$userId, 'role_id'=>$this->id));

    }

    public function attachBunchUser($userIds)
    {
        //consider insert as select.
        foreach ($userIds as $userId) {
            $this->attachUser($userId);
        }
    }

    public function detachPrivileges()
    {
        $rolePrivilegeTable = TableGatewayFactory::getRolePrivilegeTable();
        $rolePrivilegeTable->getTableGateway()->delete(array('role_id'=>$this->id));
    }
}
